package com.kunpeng.weibo.weixin.api;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXImageObject;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.platformtools.Util;

public class SendImage_API extends Request_API {

	private final int	THUMB_SIZE	= 150;

	public SendImage_API(IWXAPI api) {
		super(api);
	}

	public void sendImageWithBitmap(Bitmap bitmap, String title, String desc) throws IllegalArgumentException {
		if (bitmap == null) {
			throw new IllegalArgumentException();
		}
		WXImageObject imgObj = new WXImageObject(bitmap);

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;
		
		if (title != null) {
			msg.title = title;
		}
		if (desc != null) {
			msg.description = desc;
		}

		Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
		bitmap.recycle();
		msg.thumbData = Util.bmpToByteArray(thumbBmp, true);  // 设置缩略图

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("img");
		req.message = msg;
		mWeixinApi.sendReq(req);
	}

	public void sendImageWithPath(String path, String title, String desc) throws FileNotFoundException, IllegalArgumentException {
		if (path == null || path.equals("")) {
			throw new IllegalArgumentException();
		}
		File file = new File(path);
		if (!file.exists()) {
			throw new FileNotFoundException();
		}

		WXImageObject imgObj = new WXImageObject();
		imgObj.setImagePath(path);

		WXMediaMessage msg = new WXMediaMessage();
		msg.mediaObject = imgObj;
		
		if (title != null) {
			msg.title = title;
		}
		if (desc != null) {
			msg.description = desc;
		}

		Bitmap bmp = BitmapFactory.decodeFile(path);
		Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
		bmp.recycle();
		msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

		SendMessageToWX.Req req = new SendMessageToWX.Req();
		req.transaction = buildTransaction("img");
		req.message = msg;
		mWeixinApi.sendReq(req);
	}

	public void sendImageWithUrl(String url, String title, String desc) throws IllegalArgumentException {
		if (url == null || url.equals("") || !url.startsWith("http")) {
			throw new IllegalArgumentException();
		}
		try {
			WXImageObject imgObj = new WXImageObject();
			imgObj.imageUrl = url;

			WXMediaMessage msg = new WXMediaMessage();
			msg.mediaObject = imgObj;
			
			if (title != null) {
				msg.title = title;
			}
			if (desc != null) {
				msg.description = desc;
			}

			Bitmap bmp = BitmapFactory.decodeStream(new URL(url).openStream());
			Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true);
			bmp.recycle();
			msg.thumbData = Util.bmpToByteArray(thumbBmp, true);

			SendMessageToWX.Req req = new SendMessageToWX.Req();
			req.transaction = buildTransaction("img");
			req.message = msg;
			mWeixinApi.sendReq(req);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
